<?php
namespace app\api\controller;

use think\Db;


/**
 * @title 兴趣搜索
 * @description 接口说明
 * @group api
 *
 */
class SearchTag extends Base
{
    protected $_order = 'id desc';

    /**
     * @title 获取
     * @description 接口说明
     * @author hardy
     * @url /api/SearchTag
     * @method GET
     *
     * @param name:id type:int require:0 default:1 other: desc:唯一ID，获取某条记录详情，则为必填，id和page选填其一
     * @param name:page type:int require:0 default:1 other: desc:页码数 获取第二页以上的数据必填，默认值为1，id和page选填其一
     * @param name:size type:int require:0 default:20 other: desc:每一页的数量，默认20
     *
     * @return name:搜索名称
     * @return tags:标签
     *
     * @return list:消息列表,有page参数则返回值list，list是一个数组，
     * @return size:每页最大数，默认20，可由前端传递参数决定 page参数返回值，
     * @return page:当前页，page参数返回值，
     * @return count:总记录数， page参数返回值，
     */
    public function gets()
    {
        parent::gets();

    }
    /**
     * @title 热门标签返回文章内容
     * @description 接口说明
     * @author wang
     * @url /api/SearchTag(version=2)
     * @method GET
     *
     * @param name:id type:int require:0 default:1 other: desc:唯一ID，获取某条记录详情，则为必填，
     * @param name:page type:int require:0 default:1 other: desc:页码数 获取第二页以上的数据必填，默认值为1，id和page选填其一
     * @param name:size type:int require:0 default:20 other: desc:每一页的数量，默认20
     *
     * @return status:是否成功
     * @return data:成功后返回的数据
     *

     */
    public function gets_v2()
    {

        $size = input('get.size');

        $page = input('get.page');
        if (!isset($size))
            $size = 20;
        if (!isset($page))
            $page = 1;

        $map = [];
        $this->where($map);

        $a = Db::table('tp_alliance')->field('cover_link,change_time,id,title,tags,content,tab')->where($map)->fetchSql(true)->select();

        $b = Db::table('tp_news')->field('cover_link,change_time,id,title,tags,content,tab')->where($map)->fetchSql(true)->select();
        $c = Db::table('tp_business')->field('cover_link,change_time,id,title,tags,content,tab')->where($map)->fetchSql(true)->select();

        $result = Db::table('tp_policy_consult')->field('cover_link,change_time,id,title,tags,content,tab')->union([$a, $b, $c])->where($map)->order('change_time desc')->limit(($page - 1) * $size, $size)->select();
        foreach ($result as $k => $v) {

            $result[$k]['change_time'] = date('Y-m-d H:i:s', $result[$k]['change_time']);


        }
      $count =Db::table('tp_policy_consult')->field('cover_link,change_time,id,title,tags,content,tab')->union([$a, $b, $c])->where($map)->select();



        $data = [
            'page' => $page,
            'size' => $size,
            'count' => count($count),
            'list' => $result,
        ];
        $this->responseSuccess($data);
    }
    protected function where(&$map)
    {
        $map[] = [ 'delete_time','=',null];
        $id = input('get.id');
        if (isset($id)){

            $tag = new \app\common\model\SearchTag();
            $data = $tag->field('tags,name')->where('id',input('get.id'))->find();
            foreach ($data['tags'] as $v){
                $map[] = ['title|tags','like',"%".$v."%"];
            }
            $map[] = ['title|tags','like',"%".$data['name']."%"];
        }
        $keys = input('get.keys');
        if (isset($keys)){

            $map[] = ['title|tags','like',"%".$keys."%"];
        }



    }
}